package com.chat;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 设置页面
 */
public class Setting extends AppCompatActivity {

    EditText ip,port;
    Button sure,cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        init();
    }

    public void init(){
        ip = findViewById(R.id.setting_ip);
        port = findViewById(R.id.setting_port);
//        ip.setText(ChatObjectServer.getServer().getHost());
//        port.setText(ChatObjectServer.getServer().getPort());
        sure = findViewById(R.id.setting_sure);
        cancel = findViewById(R.id.setting_cancel);

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strHost = ip.getText().toString().trim();
                String strPort = port.getText().toString().trim();
                //启动网络服务
//                ChatObjectServer.getServer().setHost(strHost);
//                ChatObjectServer.getServer().setPort(strPort);
                save(strHost,strPort);
                finish();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 保存网络信息
     * @param host
     * @param port
     */
    public void save(String host,String port){
        SharedPreferences sharedPreferences = getSharedPreferences("chat",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("host",host);
        editor.putString("port",port);
        editor.commit();
//        editor.apply();
    }

}